package com.happy.base.redis;

import com.happy.Test.TestUserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private Set<TestUserBean> users = new HashSet<TestUserBean>();


//    @CachePut(value = "user", key = "'User:'+#user.id")
//    public TestUserBean addUser(TestUserBean user) {
//        users.add(user);
//        return user;
//    }

    @Cacheable(value = "user", key = "'User:'+#id")
    public TestUserBean addUser(int id, String name) {
        TestUserBean user = new TestUserBean(id, name);
        users.add(user);
        return user;
    }

    @Cacheable(value = "user", key = "'User:'+#id")
    public TestUserBean getStudent(int id) {
        System.out.println("not in redis cache");
        for (TestUserBean user : users) {
            if (user.getId() == id) return user;

        }
        return null;
    }

}